package Engine.AIUGame
{
	import Engine.AIUGame.Keys.Keyboard;
	
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	/**
	 * 位图动画 全局数据中心
	 */
	public class AIGDate
	{
		
		public static var Game:AIGame;
		public static var GameView:AIView = null;
		
		
		public static var paused:Boolean;
		public static var timeScale:Number;
		public static var elapsed:Number;
		public static var GameWidth:Number;
		public static var GameHeight:Number;
		
		public static var worldBounds:Rectangle;		//决定游戏中碰撞检测的范围
		public static var worldDivisions:uint;		//碰撞检测的频率
		
		public static var keys:Keyboard;
		
		
		
		static public function init(game:AIGame,Width:uint,Height:uint):void
		{
			AIGDate.Game = game;
			AIGDate.GameWidth = Width;
			AIGDate.GameHeight = Height;
			AIGDate.GameView = new AIView(0,0,1000,700);
		}
		
		
		static internal function reset():void
		{
			AIGDate.paused = false;
			AIGDate.timeScale = 1.0;
			AIGDate.elapsed = 0;
			AIGDate.worldBounds = new Rectangle(-10,-10,AIGDate.GameWidth+20,AIGDate.GameHeight+20);
			AIGDate.worldDivisions = 6;
			keys = new Keyboard();
		}
		
		
		static public function addCameraView():void
		{
			AIGDate.Game.addChild(AIGDate.GameView._flashSprite);
		}
		
		static public function removeCameraView():void
		{
			if(AIGDate.GameView._flashSprite.parent != null)
			{
				AIGDate.Game.removeChild(AIGDate.GameView._flashSprite);
			}
		}
		
		
		static public function updateViewCamera():void
		{
			AIGDate.GameView.update();
		}
		
		
		/**
		 * 速度公式
		 * @param Velocity 原始速度
		 * @param Acceleration 加速度
		 * @param Drag 阻力
		 * @param Max 允许速度的最大值
		 * 
		 * @return 返回速度 Number
		 */
		static public function computeVelocity(Velocity:Number, Acceleration:Number=0, Drag:Number=0, Max:Number=10000):Number
		{
			if(Acceleration != 0)
				Velocity += Acceleration*AIGDate.elapsed;
			else if(Drag != 0)
			{
				var drag:Number = Drag*AIGDate.elapsed;
				if(Velocity - drag > 0)
					Velocity = Velocity - drag;
				else if(Velocity + drag < 0)
					Velocity += drag;
				else
					Velocity = 0;
			}
			if((Velocity != 0) && (Max != 10000))
			{
				if(Velocity > Max)
					Velocity = Max;
				else if(Velocity < -Max)
					Velocity = -Max;
			}
			return Velocity;
		}
		
		
		/**
		 * 拷贝生成一张新的位图数据
		 * @param copyPixels 旧的位图数据
		 * @param Reverse 是否需要翻转
		 */
		static public function addBitmap(copyPixels:BitmapData, Reverse:Boolean=false):BitmapData
		{
			var pixels:BitmapData = null;
			
			if(Reverse)
			{
				pixels= new BitmapData(copyPixels.width<<1,copyPixels.height,true,0x00000000);
				pixels.draw(copyPixels);
				var mtx:Matrix = new Matrix();
				mtx.scale(-1,1);
				mtx.translate(pixels.width,0);
				pixels.draw(copyPixels,mtx);
			}
			else
			{
				pixels = new BitmapData(copyPixels.width,copyPixels.height,true,0x00000000);
				pixels.draw(copyPixels);
			}
			return pixels;
		}
		
		/**
		 * 创建一张新的位图
		 * 
		 */
		static public function createBitmap(Width:uint, Height:uint, Color:uint):BitmapData
		{
			return (new BitmapData(Width,Height,true,Color));
		}
		
		
		static public function max(Number1:Number,Number2:Number):Number
		{
			return (Number1 >= Number2)?Number1:Number2;
		}
		
		/**
		 * 检查对象之间的碰撞
		 */
		static public function collide(ObjectOrGroup1:AIBasic=null, ObjectOrGroup2:AIBasic=null, NotifyCallback:Function=null):Boolean
		{
			return overlap(ObjectOrGroup1,ObjectOrGroup2,NotifyCallback,AIObject.separate);
		}
		
		
		static public function overlap(ObjectOrGroup1:AIBasic=null,ObjectOrGroup2:AIBasic=null,NotifyCallback:Function=null,ProcessCallback:Function=null):Boolean
		{
			if(ObjectOrGroup1 == null)
				ObjectOrGroup1 = AIGDate.Game.gameGroup;
			if(ObjectOrGroup2 === ObjectOrGroup1)
				ObjectOrGroup2 = null;
			AIQuadTree.divisions = AIGDate.worldDivisions;
			var quadTree:AIQuadTree = new AIQuadTree(AIGDate.worldBounds.x,AIGDate.worldBounds.y,AIGDate.worldBounds.width,AIGDate.worldBounds.height);
			quadTree.load(ObjectOrGroup1,ObjectOrGroup2,NotifyCallback,ProcessCallback);
			var result:Boolean = quadTree.execute();
			quadTree.destroy();
			return result;
		}
		
		
		
		
		
		
		
		
		
		
	}
}